package com.briup.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class TempMax extends Configured implements Tool {
    static class MaxWeatherMapper
            extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String[] str = value.toString().split(",");
            context.write(new IntWritable(Integer.parseInt(str[0])),
                    new IntWritable(Integer.parseInt(str[1])));
        }
    }

    static class MaxWeatherReducer
            extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
        @Override
        protected void reduce(IntWritable key, Iterable<IntWritable> values,
                              Context context)
                throws IOException, InterruptedException {
            int max = 0;
            for (IntWritable iw : values) {
                if (iw.get() > max) {
                    max = iw.get();
                }
            }
            context.write(key, new IntWritable(max));
        }
    }

    @Override
    public int run(String[] strings) throws Exception {
        //构建配置对象，读取hadoop的配置文件，如hdfs-site.xml、yarn-site.xml、
        //core-site.xml、mapred-site.xml等
        Configuration conf = getConf();
        Path input = new Path("hdfs://192.168.10.11:9000/weadata");
        Path output = new Path("hdfs://192.168.10.11:9000/weaoutput");
        //指定需要处理数据的hdfs文件路径
        //String input = conf.get("input");
        //指定Mapreduce程序执行结果存放的路径
        //String output = conf.get("output");
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.10.11:9000"), conf);
        if (fileSystem.exists(output)) fileSystem.delete(output,true);
        //构建作业程序
        Job job = Job.getInstance(conf);
        //指定作业运行的类
        job.setJarByClass(this.getClass());
        //指定提交作业的名字
        job.setJobName("MaxWeather");
        //指定Mapper程序是哪个类
        job.setMapperClass(MaxWeatherMapper.class);
        //指定Mapper输出的键类型
        job.setMapOutputKeyClass(IntWritable.class);
        //指定Mapper输出的值的类型
        job.setMapOutputValueClass(IntWritable.class);
        //设置Reduce的个数
        job.setNumReduceTasks(2);
        //指定Reduce程序是哪个类
        job.setReducerClass(MaxWeatherReducer.class);
        //指定Reduce程序输出的键类型
        job.setOutputKeyClass(IntWritable.class);
        //指定Reduce程序输出值的类型
        job.setOutputValueClass(IntWritable.class);
        //指定文件和Mapper之间对接的处理器，处理器负责将文件每行内容按照特定规则转化为键值
        //键值对交给Mapper
        //TextInputFormat读取每一行数据起始位置为偏移位置，整行内容为值
        job.setInputFormatClass(TextInputFormat.class);
        //指定Reduce输出结果和结果存储文件的处理器，处理器负责将Reduce输出键值对按照特定规则写入结果文件
        job.setOutputFormatClass(TextOutputFormat.class);
        //负责读数据的处理器绑定读取文件路径
        TextInputFormat.addInputPath(job, input);
        //负责写数据的处理器绑定存储结果文件路径
        TextOutputFormat.setOutputPath(job, output);
        //提交作业给集群，true表示集群执行程序结束提交窗口命令行
        return job.waitForCompletion(true) ? 0 : -1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(
                new ToolRunner().run(
                        new TempMax(), args));
    }
}
